#include "RenderPass.h"

mini_engine::RenderPass *mini_engine::RenderPass::m_renderPass = nullptr;
VkRenderPass mini_engine::RenderPass::renderPass;

mini_engine::RenderPass::RenderPass()
{
}

void mini_engine::RenderPass::createRenderPass()
{
    cout << "createRenderPass" << endl;
        VkAttachmentDescription colorAttachment{};
        colorAttachment.format = SwapChain::swapChainImageFormat;
        colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
        colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
        colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
        colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
        colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
        colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
        colorAttachment.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;

        VkAttachmentReference colorAttachmentRef{};
        colorAttachmentRef.attachment = 0;
        colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;

        VkSubpassDescription subpass{};
        subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
        subpass.colorAttachmentCount = 1;
        subpass.pColorAttachments = &colorAttachmentRef;

        VkRenderPassCreateInfo renderPassInfo{};
        renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
        renderPassInfo.attachmentCount = 1;
        renderPassInfo.pAttachments = &colorAttachment;
        renderPassInfo.subpassCount = 1;
        renderPassInfo.pSubpasses = &subpass;

        if (vkCreateRenderPass(LogicalDevice::logicalDevice, &renderPassInfo, nullptr, &RenderPass::renderPass) != VK_SUCCESS) {
            throw std::runtime_error("failed to create render pass!");
        }
}

mini_engine::RenderPass *mini_engine::RenderPass::getRenderPass()
{
    if (m_renderPass == nullptr)
    {
        m_renderPass = new RenderPass();
        RenderPassDestruction renderPassDestruction;
    }
    return m_renderPass;
}

mini_engine::RenderPass::~RenderPass()
{
}

mini_engine::RenderPass::RenderPassDestruction::~RenderPassDestruction()
{
    if (m_renderPass != nullptr)
    {
        delete m_renderPass;
        m_renderPass = nullptr;
    }
}
